package com.dhh.dao;

import java.util.ArrayList;
import java.util.List;
import com.dhh.vo.Food;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class FoodDAO {
	private DataBaseHelper db;
	
	public FoodDAO(Context ctx){
		db = new DataBaseHelper(ctx);
	}
	
	public FoodDAO(DataBaseHelper db){
		this.db = db;
	}
	
	public Food getFoodByID(String id){		
		if(id==null || id.trim().length()==0)
			return null;
		Food food = new Food();
		
		try{
			db.openDataBase();
			String whereSQL = "_id='"+id+"'";
			Cursor cursor = db.getReadableDatabase().query(true, "FOOD_DESC", Food.getColumns(), whereSQL,null, null, null, null, null);				
			if(cursor!=null && cursor.moveToFirst()){
				food.setId(cursor.getString(0));
				food.setFoodName(cursor.getString(1));
				food.setFoodGroup(cursor.getString(2));
				food.setProtein(cursor.getString(3));
				food.setFat(cursor.getString(4));
				food.setCarb(cursor.getString(5));
			}
		}catch(Exception e){
			Log.w("FoodDAO", e);
		}finally{
			if(db!=null) db.close();
		}
		return food;
	}
	
	public List<Food> listFoods(String foodGroup,String foodName){
		List<Food> foods = new ArrayList<Food>();
		try{
			db.openDataBase();
			String whereSQL = "1=1 ";
			if(foodGroup!=null && foodGroup.trim().length()>0){
				//whereSQL += "and food_group='"+foodGroup+"' ";
			}
			if(foodName!=null && foodName.trim().length()>0){
				whereSQL += "and food_name like '"+foodName+"%' ";
			}
			Cursor cursor = db.getReadableDatabase().query(true, "FOOD_DESC", Food.getColumns(), whereSQL,null, null, null, null, "10");				
			if(cursor!=null){
				cursor.moveToFirst();
				do{
					Food food = new Food();
					food.setId(cursor.getString(0));
					food.setFoodName(cursor.getString(1));
					food.setFoodGroup(cursor.getString(2));
					food.setProtein(cursor.getString(3));
					food.setFat(cursor.getString(4));
					food.setCarb(cursor.getString(5));
					foods.add(food);
				}while(cursor.moveToNext());
				cursor.close();
			}			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return foods;
	}
	
	public List<Food> listFoods(String foodGroup,String foodName,int page){
		int pageCnt = 20;
		int start = pageCnt * (page-1);
		String limit = start+","+pageCnt;
		
		
		List<Food> foods = new ArrayList<Food>();
		try{
			db.openDataBase();
			String whereSQL = "1=1 ";
			if(foodGroup!=null && foodGroup.trim().length()>0){
			}
			if(foodName!=null && foodName.trim().length()>0){
				whereSQL += "and food_name like '%"+foodName+"%' ";
			}
			Cursor cursor = db.getReadableDatabase().query(true, "FOOD_DESC", Food.getColumns(), whereSQL,null, null, null, "food_name,_id", limit);				
			if(cursor!=null){
				cursor.moveToFirst();
				do{
					Food food = new Food();
					food.setId(cursor.getString(0));
					food.setFoodName(cursor.getString(1));
					food.setFoodGroup(cursor.getString(2));
					food.setProtein(cursor.getString(3));
					food.setFat(cursor.getString(4));
					food.setCarb(cursor.getString(5));
					foods.add(food);
				}while(cursor.moveToNext());
				cursor.close();
			}			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return foods;
	}
	
	public boolean addFood(Food food){
		boolean isSuc = false;
		ContentValues initialValues = createContentValues(food);
		try{
			db.getWritableDatabase().insert("food_desc", null, initialValues);
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		return isSuc;
	}
	
	private ContentValues createContentValues(Food food){
		ContentValues values = new ContentValues();
		values.put(Food.C_FOOD_NAME, food.getFoodName());
		values.put(Food.C_FOOD_GROUP, food.getFoodGroup());
		values.put(Food.C_FAT, food.getFat());
		values.put(Food.C_PROTEIN, food.getProtein());
		values.put(Food.C_CARB, food.getCarb());
		return values;
	}

	
	public void open(){
		db.openDataBase();
	}
	
	public void close(){
		db.close();
	}
}
